﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.Dictionary;
using BF.Model;
using WebDataControls;
using BF.BLL.Dictionary;
using Base.Core.UnityFactory;
using System.Configuration;

namespace BF.Web.pages.Dictionary.DictManagement
{
    public partial class DictEdit : BasePage
    {
        BLL.Dictionary.Dic_DictBLL bll = IOCFactory.GetIOCResolve<Dic_DictBLL>();   //业务逻辑层


        #region 系统事件
        protected void Page_Load(object sender, EventArgs e)                        //PageLoad
        {
            if (!IsPostBack)
            {
                //if (Opt == "Add")                                                   //按钮初始化
                //{
                //    btnSave.Visible = false;
                //}
                //else
                //{
                //    btnAdd.Visible = false;
                //    sTableCode.Enabled = false;
                //}

                switch (Opt)
                {
                    case "Add": btnSave.Visible = false; break;
                    case "Update": btnAdd.Visible = false; sTableCode.Enabled = false; break;
                    case "View": btnAdd.Visible = false; btnSave.Visible = false; btnReset.Visible = false; break;
                }


                if (!string.IsNullOrEmpty(ParentID))                                //添加明细UI
                {
                    dic_Dict model = bll.GetModel(new Guid(ParentID));              //初始化大类值
                    if (model.sTableCode == "GEN")
                    {
                        sTableCode.Text = model.sCode;
                        sTableName.Text = model.sName;
                    }
                    else
                    {
                        sTableCode.Text = model.sTableCode;
                        sTableName.Text = model.sTableName;
                    }
                }

                if (Opt == "Update")  //修改明细界面UI
                {
                    dic_Dict model = bll.GetModel(new Guid(GKey));
                    WebUIHelper.SetControlsValue(model, "dic_Dict", this.Form);
                    SysAreaControl1.SetSysAreaValue(model.sAreaCode);
                    sCode.Enabled = false;
                }
                else if (Opt == "View")
                {
                    dic_Dict model = bll.GetModel(new Guid(GKey));
                    WebUIHelper.SetControlsValue(model, "dic_Dict", this.Form);
                    SysAreaControl1.SetSysAreaValue(model.sAreaCode);
                    sCode.ReadOnly = sName.ReadOnly = sLookUp.ReadOnly = sMemo.ReadOnly = sFlag1.ReadOnly = sFlag2.ReadOnly = sFlag3.ReadOnly = sFlag4.ReadOnly = sFlag5.ReadOnly = true;
                    ScriptManager.RegisterStartupScript(this, GetType(), "", "winer.win.title='查看';", true);
                }

            }

            SetHideAddUrl();//SetUrl
        }

        protected void btnAdd_Click(object sender, EventArgs e)                     //添加
        {
            if (sCode.Text.Trim().ToLower() == "gen")
            {
                Messages("编码名称不可为\"GEN\"！");
                return;
            }

            bool isExit = bll.GetList(p =>                                          //验证
                p.sTableCode == sTableCode.Text.Trim() &&
                p.sCode == sCode.Text.Trim()).Count > 0;
            if (isExit)
            {
                Messages("编码已经存在！");
                return;
            }

            dic_Dict model = new dic_Dict();                                        //添加
            model = (dic_Dict)WebUIHelper.GetControlsModel(model, "dic_Dict", this.Form);
            model.gKey = Guid.NewGuid();  //GUID
            //model.sAreaCode = GetSysParam("AreaCode");   //地区编码
            model.isSystem = 1;                                       //系统使用标志
            model.iUse = 1;                                       //用户使用标志
            model.dInsert = DateTime.Now;                         //创建日期
            model.dLastEdit = model.dInsert;                      //最后修改日期
            model.iLevel = sTableCode.Text == "GEN" ? 0 : 1;      //目录级别
            if (model.iLevel == 1)
            {
                int? sort = bll
                .GetList(p => p.sTableCode == model.sTableCode)
                .Max(P => P.iSort);  //model.iSort
                model.iSort = (sort ?? -1) + 1;//顺序
            }

            if (bll.Add(model))                                                     //成功跳转
            {
                BF.Web.App_Code.UICommon.UpdateDictList();        //更新内存中的标准字典表
                Messages("新增成功！", "function(){winer.RefurbishParent(); winer.win.title='字典修改';window.location.href='DictEdit.aspx?opt=Update&gKey=" + model.gKey.ToString() + "'}", this);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)                    //修改
        {
            bool isExit = bll.GetList(p =>                                          //验证
            p.sTableCode == sTableCode.Text.Trim() &&
            p.sCode == sCode.Text.Trim() &&
            p.gKey != new Guid(GKey)).Count > 0;
            if (isExit)
            {
                Messages("编码已经存在！");
                return;
            }

            dic_Dict model = bll.GetModel(new Guid(GKey));                          //修改
            model = (dic_Dict)WebUIHelper.GetControlsModel(model, "dic_Dict", this.Form);
            model.dLastEdit = DateTime.Now; //最后编辑日期
            if (bll.Update())
            {
                BF.Web.App_Code.UICommon.UpdateDictList();       //更新内存中的标准字典表
                Messages("修改成功！", "function(){winer.RefurbishParent();}", this);
            }
        }
        #endregion


        #region 属性
        private string Opt                                                         // 编辑类型
        {
            get { return Request.QueryString["opt"]; }
        }

        private string ParentID                                                    //大类ID
        {
            get { return Request.QueryString["parentid"]; }
        }

        private string GKey                                                        //修改数据的GUID
        {
            get { return Request.QueryString["gKey"]; }
        }
        #endregion


        #region 自定义方法
        protected void SetHideAddUrl()                                              //把新增页面URL路径写入隐藏域以便前台调用  
        {
            if (Opt == "Add")                     //"新增"状态
                hidAddUrl.Value = Request.RawUrl;
            else
                if (sTableCode.Text == "GEN")     //"修改"状态
                    hidAddUrl.Value = "DictEdit.aspx?opt=Add";//跳转到大类
                else                                           //跳转到明细
                {
                    List<dic_Dict> modelList = bll.GetList(p => p.sTableCode == sTableCode.Text);
                    if (modelList.Count > 0)
                    {
                        hidAddUrl.Value = "DictEdit.aspx?opt=Add&parentid=" + modelList[0].gKey;
                    }
                }
        }
        #endregion
    }
}